package com.yanqu.road.server.cmd.activity.washe;

import com.yanqu.road.entity.activity.washe.data.WasheHistoryRank;
import com.yanqu.road.entity.enums.eSeasonRewardType;
import com.yanqu.road.entity.rank.xrank.XSeasonUserRank;
import com.yanqu.road.logic.pb.XRankPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.WasheProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.washe.WasheMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;

import java.util.Map;

@Cmd(code = GameProtocol.S_WASHE_GET_HISTORY_CHAMPION, desc = "")
public class WasheGetHistoryChampionCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        WasheProto.WasheGetHistoryChampionRespMsg.Builder builder = WasheProto.WasheGetHistoryChampionRespMsg.newBuilder();

        for (Map.Entry<Integer, Map<Integer, WasheHistoryRank>> mapEntry : WasheMgr.getHistoryRankMap().entrySet()) {
            WasheProto.WasheChampionMsg.Builder championBuilder = WasheProto.WasheChampionMsg.newBuilder();
            int seasonId = mapEntry.getKey();
            Map<Integer, WasheHistoryRank> rankMap = mapEntry.getValue();
            championBuilder.setSeasonId(seasonId);
            // 赛季冲榜
            WasheHistoryRank washeScoreRank = rankMap.get(eSeasonRewardType.WasheScoreRank.getIntValue());
            if (washeScoreRank != null) {
                XSeasonUserRank topRank = washeScoreRank.getTopRank();
                if (topRank != null) {
                    championBuilder.setRank(XRankPb.parseXUserRankMsg(topRank, false));
                }
            }
            // 巅峰赛
            WasheHistoryRank jinBiaoPeakRank = rankMap.get(eSeasonRewardType.JinBiaoPeakRank.getIntValue());
            if (jinBiaoPeakRank != null) {
                XSeasonUserRank topRank = jinBiaoPeakRank.getTopRank();
                if (topRank != null) {
                    championBuilder.setJinBiaoRank(XRankPb.parseXUserRankMsg(topRank, false));
                }
            }
            builder.addChampion(championBuilder);
        }

        player.sendPacket(ClientProtocol.U_WASHE_GET_HISTORY_CHAMPION, builder);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
